package store

import (
	"sync"
	"time"

	"servermerge/internal/model"
)

type TaskStore struct {
	mu   sync.RWMutex
	data map[string]*model.MergeTask
}

var globalStore = &TaskStore{data: make(map[string]*model.MergeTask)}

func SaveTask(t *model.MergeTask) {
	globalStore.mu.Lock()
	defer globalStore.mu.Unlock()
	globalStore.data[t.ID] = t
}

func UpdateTask(id string, fn func(*model.MergeTask)) {
	globalStore.mu.Lock()
	defer globalStore.mu.Unlock()
	if t, ok := globalStore.data[id]; ok {
		fn(t)
		t.UpdatedAt = time.Now()
	}
}

func GetTask(id string) *model.MergeTask {
	globalStore.mu.RLock()
	defer globalStore.mu.RUnlock()
	return globalStore.data[id]
}
